//
//  QYGRechargeCell.m
//  wawaji_ios
//
//  Created by 云+物娱 on 2017/12/1.
//  Copyright © 2017年 yiguo qi. All rights reserved.
//

#import "QYGRechargeCell.h"
#import "QYGCoinRecordModel.h"

@interface QYGRechargeCell()

@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *detailLabel;
@property (nonatomic, strong) UIButton *sureBtn;
@property (nonatomic, strong) UIView *line;
@end

@implementation QYGRechargeCell

- (void)setCoinListModel:(QYGCoinListModel *)coinListModel{
    _coinListModel = coinListModel;
    
    NSString *coinStr = [NSString stringWithFormat:@"%ld",coinListModel.coin];
    NSString *appendStr = @"金币";
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@",coinStr,appendStr]];
    
    [AttributedStr addAttribute:NSFontAttributeName
                          value:ScaleFont(14)
                          range:NSMakeRange(coinStr.length, appendStr.length)];
    
    self.nameLabel.attributedText = AttributedStr;
    
    NSString *rewardStr = [NSString stringWithFormat:@"(赠送%ld金币)",coinListModel.coin_award];
    self.detailLabel.text = rewardStr;
    self.detailLabel.hidden = !coinListModel.coin_award;
    [self.sureBtn setTitle:[NSString stringWithFormat:@"¥%.2f",coinListModel.money] forState:UIControlStateNormal];
    
    [self.nameLabel sizeToFit];
    [self.detailLabel sizeToFit];
}


#pragma mark target
- (void)sureBtnClick:(UIButton *)btn{
    QYGRechargeCell *rechargeCell = (QYGRechargeCell *)[btn superview];
    if(self.sureBlock){
        
      self.sureBlock(rechargeCell.item);
    }
}

#pragma mark setUI

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        
        [self initSubView];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}
- (void)initSubView{
    
    [self addSubview:self.iconView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.detailLabel];
    [self addSubview:self.sureBtn];
    [self addSubview:self.line];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.iconView.x = ScaleW(20);
    self.iconView.y = (self.height - self.iconView.height) * 0.5;
    
    
    self.nameLabel.x = CGRectGetMaxX(self.iconView.frame) + ScaleW(10);
    self.nameLabel.centerY = self.iconView.centerY;
    
    self.detailLabel.x = self.nameLabel.x;
    self.detailLabel.y = CGRectGetMaxY(self.nameLabel.frame) + ScaleW(5);
    
    self.sureBtn.x = self.width - ScaleW(20) - self.sureBtn.width;
    self.sureBtn.centerY = self.iconView.centerY - 2;
    
    self.line.x = ScaleW(10);
    self.line.height = 0.5f;
    self.line.y = self.height - self.line.height;
    self.line.width = self.width - ScaleW(10) * 2;
    
    
}
- (UIView *)line{
    if(!_line){
        _line = [SGUtil newViewWithBgColor:[UIColor colorWithHex:@"e1e4eb"]];
    }
    return _line;
}
- (UIImageView *)iconView{
    if(!_iconView){
        _iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mine_rcg_coin"]];
    }
    return _iconView;
}
- (UILabel *)detailLabel{
    if(!_detailLabel){
        _detailLabel = [UILabel labelWithText:@"" setFont:ScaleFont(10) setTextColor:[UIColor colorWithHex:@"09bb07"]];
        _detailLabel.numberOfLines = 1;
    }
    return _detailLabel;
}
- (UILabel *)nameLabel{
    
    if(!_nameLabel){
        _nameLabel = [UILabel labelWithText:@"" setFont:ScaleFont(20) setTextColor:[UIColor colorWithHex:@"ffb52b"]];
        _nameLabel.numberOfLines = 1;
    }
    return _nameLabel;
}
- (UIButton *)sureBtn{
    if(!_sureBtn){
        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureBtn setBackgroundImage:[UIImage imageNamed:@"rechargebtnBg"] forState:UIControlStateNormal];
        [_sureBtn setTitle:@"¥10.0" forState:UIControlStateNormal];
        _sureBtn.titleLabel.font = ScaleBoldFont(16);
        _sureBtn.size = CGSizeMake(ScaleW(88), ScaleW(36));
        [_sureBtn addTarget:self action:@selector(sureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}
@end
